%% ICA of two datasets: decomposing in control components
% clear; 
% clc;
% 
% [ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
% % EEG1 = pop_loadset('filepath','D:\ECoG\Data\CW Sphere Ambiguous Response epochs.set');
% EEG = pop_loadset('filepath','D:\ECoG\Data\CW Sphere Unambiguous Response epochs.set');
% load('CW_Chanlocs.mat');
% 
% elecSelection = {'T18', 'T25', 'sTa3', 'OcO2','OcO3','OcO4','OcO5','OcO9','Oc10','Oc11','T09','T07','P02','P09','P10'}; %'P10'
% for i = 1:length(elecSelection)
%     esIdx(i,:) = find(strcmp(elecSelection{i},{EEG.chanlocs.labels}));
% end;
% 
% OUT_EEG = pop_runica( EEG, 'icatype', 'runica','chanind', esIdx, 'extended', 5, 'maxsteps', 5000) %
% 
% unmixing = OUT_EEG.icaweights * OUT_EEG.icasphere;
% EEG1 = pop_loadset('filepath','D:\ECoG\Data\CW Sphere Ambiguous Response epochs.set');
% amb_data = EEG1.data(esIdx,:,:);
% amb_act = []; 
% for i = 1:43;
% amb_act(:,:,i) = unmixing * amb_data(:,:,i);
% end
% 
% % selectedChanlocs = CW_Chanlocs(esIdx);
% % 
% % % for i = 1:length(data(:,1));
% % %     smoothdata(i,:) = GaussianSmooth(data(i,:)', 30);
% % % end
% % 
% % [weights,sphere,compvars,bias,signs,lrates,activations] = runica(data3d,'extended', 5, 'maxsteps',5000);
% % 
% % % t = tfc.times - 5000;
% % 
% % times = [EEG.times]; %,EEG.times+10000
% % 
% % Plot components
% figure;
% unamb_actdata = mean(OUT_EEG.icaact,3);
% for i = 1:length(unamb_actdata(:,1));
%     h = subplot(5,3,i);
%      plot (OUT_EEG.times, unamb_actdata(i,:));
%     xlim([-4000 4000]);
%       title (['Component ' num2str(i)]);
%       set(h, 'xgrid', 'on');
% end
% 
% figure;
% amb_actdata = mean(amb_act,3);
% for i = 1:length(amb_act(:,1));
%     h = subplot(5,3,i);
%      plot (OUT_EEG.times, amb_actdata(i,:));
%     xlim([-4000 4000]);
%       title (['Component ' num2str(i)]);
%       set(h, 'xgrid', 'on');
% end
% 
% % Compare components (smoothed)
% figure;
% plot(OUT_EEG.times(1900:3220),GaussianSmooth(unamb_actdata(1,1900:3220)',20),'r'); 
% hold on;
% plot(OUT_EEG.times(1900:3220),GaussianSmooth(amb_actdata(1,1900:3220)',20),'b');
% title ([ 'Subject CW - SphereResponses - Comparison component 1 of Unambiguous (red) and Ambiguous (blue) - Smoothed'])
% 
% 
% % Plot weights
% figure;
% imagesc(OUT_EEG.icaweights, [-1,1])
% colorbar; 
% ylabel ('components'); xlabel ('electrodes');
% set(gca,'xTick',1:1:15);
% set(gca,'xTickLabel',elecSelection);
% 
% %Plot ERSPs
% EEG1.icaact = amb_act;
% EEG1.icaweights = EEG.icaweights;
% EEG1.icasphere = EEG.icasphere;
% 
% % In subplots
% figure;
% for i = 1:length(amb_act(:,1,1));
%     h = subplot(5,3,i);
% pop_newtimef(OUT_EEG, 0, i, [-4000 4000],0,'plotitc','off', 'maxfreq',130);
% end
% 
% % Individual plots
% figure; 
% pop_newtimef(EEG1, 0, 3, [-4000 4000],0,'plotitc','off', 'maxfreq',130);
% title ('Subject CW - SphereAmbiguousResponse - Comp 3')
% 
% 
% return;


%% ICA of concatenated electrodes 
% clear; 
% clc;
% 
% [ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
% EEG1 = pop_loadset('filepath','D:\ECoG\Data\CW Sphere Ambiguous Response epochs.set');
% EEG2 = pop_loadset('filepath','D:\ECoG\Data\CW Sphere Unambiguous Response epochs.set');
% load('CW_Chanlocs.mat');
% 
% EEG1.data(97:192,:,:) = EEG2.data;
% EEG1.chanlocs(:,97:192,:) = EEG2.chanlocs;
% 
% elecSelection = {'T18', 'T25', 'sTa3', 'OcO2','OcO3','OcO4','OcO5','OcO9','Oc10','Oc11','T09','T07','P02','P09','P10'}; %'P10'
% for i = 1:length(elecSelection)
%     esIdx(i,:) = find(strcmp(elecSelection{i},{EEG1.chanlocs.labels}));
% end;
% 
% esIdx(16:30,1) = esIdx(1:15,2);
% esIdx(1:15,2) = NaN;
% 
% OUT_EEG = pop_runica( EEG1, 'icatype', 'runica','chanind', esIdx(:,1), 'extended', 5, 'maxsteps', 5000) %
% 
% % Plot components
% figure;
% 
% actdata = mean(OUT_EEG.icaact,3);
% for i = 1:length(actdata(:,1));
%     h = subplot(6,5,i);
%      plot (OUT_EEG.times, actdata(i,:));
%     xlim([-4000 4000]);
%       title (['Component ' num2str(i)]);
%       set(h, 'xgrid', 'on');
% end
% 
% % Plot weights
% figure;
% imagesc(OUT_EEG.icaweights, [-1,1])
% colorbar; 
% ylabel ('components'); xlabel ('electrodes');
% set(gca,'xTick',1:1:15);
% set(gca,'xTickLabel',{elecSelection,elecSelection});
% 
% weightsshuf = []; %plot weights of two conditions for each electrode next to eachother
% for j = 1:15;
%     weightsshuf = [weightsshuf, OUT_EEG.icaweights(:,j), OUT_EEG.icaweights(:,j+15)];
% end
% 
% figure;
% imagesc(weightsshuf, [-1,1])
% colorbar; 
% ylabel ('components'); xlabel ('electrodes');
% set(gca,'xTick',1:2:30);
% set(gca,'xTickLabel',elecSelection);
% 
% return;

%% ICA with concatenated conditions

% clear; 
% clc;
% 
% [ALLEEG EEG CURRENTSET ALLCOM] = eeglab;
% EEG1 = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere Ambiguous Response epochs alpha band passed.set'); 
% EEG2 = pop_loadset('filepath','D:\Marije\ICA Analyze\CW Sphere Unambiguous Response epochs alpha band passed.set');
% load('CW_Chanlocs.mat');
% 
% EEG1.data(:,:,44:86) = EEG2.data;
% EEG1.trials = 86;
% EEG1.epoch(:,44:86) = EEG2.epoch;
% 
% % elecSelection = {'T18', 'T25', 'sTa3', 'OcO2','OcO3','OcO4','OcO5','OcO9','Oc10','Oc11','T09','T07','P02','P09','P10'}; %'P10'
% % for i = 1:length(elecSelection)
% %     esIdx(i) = find(strcmp(elecSelection{i},{CW_Chanlocs.labels}));
% % end;
% 
% OUT_EEG2 = pop_runica( EEG1, 'icatype', 'runica', 'maxsteps', 5000) %'chanind', esIdx

% % Plot components
% figure;
% actdata = mean(OUT_EEG.icaact,3);
% for i = 1:length(actdata(:,1));
%     h = subplot(5,3,i);
%      plot (OUT_EEG.times, actdata(i,:));
%     xlim([-4000 4000]);
%       title (['Component ' num2str(i)]);
%       set(h, 'xgrid', 'on');
% end

load('CW_Chanlocs.mat');

% Plot heads in componentplots
for i = 1:length(OUT_EEG2.data(:,1,1)); 
    newH = figure;
    values = OUT_EEG2.icaweights(i,:);
    
    topoplot(values, CW_Chanlocs, ...
        'style','both', ... % just "style: map" doesn't seem to work..
        'numcontour', 0, ...
        'electrodes', 'labels');
%     set(newH,'clim',[-2 2])
title (['Component ' num2str(i)]);
end;

% Plot weights
figure;
imagesc(OUT_EEG2.icaweights, [-1,1])
colorbar; 
ylabel ('components'); xlabel ('electrodes');
set(gca,'xTick',1:1:15);
% set(gca,'xTickLabel',elecSelection);

return;